1 using UnityEngine;
2 using
System.Collections;
3
4 ///
<summary>
5 ///
This class helps you to synchronize position, rotation and scale
6 ///
of a GameObject. It also gives you many different options to make
7 ///
the synchronized values appear smooth, even when the data is only
8 ///
send a couple of times per second.
9 ///
Simply add the component to your GameObject and make sure that
10 ///
the PhotonTransformView is added to the list of observed components
11 ///
</summary>
12 [RequireComponent(
typeof( PhotonView ) )]
13 [AddComponentMenu(
"Photon Networking/Photon Transform View")]
14 public
class PhotonTransformView : MonoBehaviour
15 {
16     
//Since this component is very complex, we seperated it into multiple objects.
17     
//The PositionModel, RotationModel and ScaleMode store the data you are able to
18     
//configure in the inspector while the control objects below are actually moving
19     
//the object and calculating all the inter- and extrapolation
20
21     
[SerializeField]
22     PhotonTransformViewPositionModel m_PositionModel =
new PhotonTransformViewPositionModel();
23
24     
[SerializeField]
25     PhotonTransformViewRotationModel m_RotationModel =
new PhotonTransformViewRotationModel();
26
27     
[SerializeField]
28     PhotonTransformViewScaleModel m_ScaleModel =
new PhotonTransformViewScaleModel();
29
30     PhotonTransformViewPositionControl m_PositionControl;
31     PhotonTransformViewRotationControl m_RotationControl;
32     PhotonTransformViewScaleControl m_ScaleControl;
33
34     PhotonView m_PhotonView;
35
36     
bool m_ReceivedNetworkUpdate = false;
37
38     
void Awake()
39     {
40         m_PhotonView = GetComponent<PhotonView>();
41
42         m_PositionControl =
new PhotonTransformViewPositionControl( m_PositionModel );
43         m_RotationControl =
new PhotonTransformViewRotationControl( m_RotationModel );
44         m_ScaleControl =
new PhotonTransformViewScaleControl( m_ScaleModel );
45     }
46
47     
void Update()
48     {
49         
if( m_PhotonView == null || m_PhotonView.isMine == true || PhotonNetwork.connected == false )
50         {
51             
return;
52         }
53
54         UpdatePosition();
55         UpdateRotation();
56         UpdateScale();
57     }
58
59     
void UpdatePosition()
60     {
61         
if( m_PositionModel.SynchronizeEnabled == false || m_ReceivedNetworkUpdate == false )
62         {
63             
return;
64         }
65
66         transform.localPosition = m_PositionControl.UpdatePosition( transform.localPosition );
67     }
68
69     
void UpdateRotation()
70     {
71         
if( m_RotationModel.SynchronizeEnabled == false || m_ReceivedNetworkUpdate == false )
72         {
73             
return;
74         }
75
76         transform.localRotation = m_RotationControl.GetRotation( transform.localRotation );
77     }
78
79     
void UpdateScale()
80     {
81         
if( m_ScaleModel.SynchronizeEnabled == false || m_ReceivedNetworkUpdate == false )
82         {
83             
return;
84         }
85
86         transform.localScale = m_ScaleControl.GetScale( transform.localScale );
87     }

88
89     ///
<summary>
90     ///
These values are synchronized to the remote objects if the interpolation mode
91     ///
or the extrapolation mode SynchronizeValues is used. Your movement script should pass on
92     ///
the current speed (in units/second) and turning speed (in angles/second) so the remote
93     ///
object can use them to predict the objects movement.
94     ///
</summary>
95     ///
<param name="speed">The current movement vector of the object in units/second.</param>
96     ///
<param name="turnSpeed">The current turn speed of the object in angles/second.</param>
97     
public void SetSynchronizedValues( Vector3 speed, float turnSpeed )
98     {
99         m_PositionControl.SetSynchronizedValues( speed, turnSpeed );
100     }
101
102     
void OnPhotonSerializeView( PhotonStream stream, PhotonMessageInfo info )
103     {
104         m_PositionControl.OnPhotonSerializeView( transform.localPosition, stream, info );
105         m_RotationControl.OnPhotonSerializeView( transform.localRotation, stream, info );
106         m_ScaleControl.OnPhotonSerializeView( transform.localScale, stream, info );
107
108         
if( m_PhotonView.isMine == false && m_PositionModel.DrawErrorGizmo == true )
109         {
110             DoDrawEstimatedPositionError();
111         }
112
113         
if( stream.isReading == true )
114         {
115             m_ReceivedNetworkUpdate =
true;
116         }
117     }
118
119     
//void OnDrawGizmos()
120     
//{
121     
// if( Application.isPlaying == false || m_PhotonView == null || m_PhotonView.isMine == true || PhotonNetwork.connected == false )
122     
// {
123     
// return;
124     
// }
125
126     
// DoDrawNetworkPositionGizmo();
127     
// DoDrawExtrapolatedPositionGizmo();
128     
//}
129
130     
void DoDrawEstimatedPositionError()
131     {
132         Vector3 targetPosition = m_PositionControl.GetNetworkPosition();
133
134         Debug.DrawLine( targetPosition, transform.position, Color.red,
2f );
135         Debug.DrawLine( transform.position, transform.position + Vector3.up, Color.green,
2f );
136         Debug.DrawLine( targetPosition, targetPosition + Vector3.up, Color.red,
2f );
137     }
138
139     
//void DoDrawNetworkPositionGizmo()
140     
//{
141     
// if( m_PositionModel.DrawNetworkGizmo == false || m_PositionControl == null )
142     
// {
143     
// return;
144     
// }
145         
146     
// ExitGames.Client.GUI.GizmoTypeDrawer.Draw( m_PositionControl.GetNetworkPosition(),
147     
// m_PositionModel.NetworkGizmoType,
148     
// m_PositionModel.NetworkGizmoColor,
149     
// m_PositionModel.NetworkGizmoSize );
150     
//}
151
152     
//void DoDrawExtrapolatedPositionGizmo()
153     
//{
154     
// if( m_PositionModel.DrawExtrapolatedGizmo == false ||
155     
// m_PositionModel.ExtrapolateOption == PhotonTransformViewPositionModel.ExtrapolateOptions.Disabled ||
156     
// m_PositionControl == null )
157     
// {
158     
// return;
159     
// }
160
161     
// ExitGames.Client.GUI.GizmoTypeDrawer.Draw( m_PositionControl.GetNetworkPosition() + m_PositionControl.GetExtrapolatedPositionOffset(),
162     
// m_PositionModel.ExtrapolatedGizmoType,
163     
// m_PositionModel.ExtrapolatedGizmoColor,
164     
// m_PositionModel.ExtrapolatedGizmoSize );
165     
//}
166 }


This class helps you to synchronize position, rotation and scale

of a GameObject. It also gives you many different options to make

the synchronized values appear smooth, even when the data is only

send a couple of times per second.

Simply add the component to your GameObject and make sure that

the PhotonTransformView is added to the list of observed components

Since this component is very complex, we seperated it into multiple objects.

The PositionModel, RotationModel and ScaleMode store the data you are able to

configure in the inspector while the control objects below are actually moving

the object and calculating all the inter- and extrapolation

These values are synchronized to the remote objects if the interpolation mode

or the extrapolation mode SynchronizeValues is used. Your movement script should pass on

the current speed (in unitssecond) and turning speed (in anglessecond) so the remote

object can use them to predict the objects movement.

The current movement vector of the object in unitssecond.

The current turn speed of the object in anglessecond.

void OnDrawGizmos()

{

if( Application.isPlaying == false || m_PhotonView == null || m_PhotonView.isMine == true || PhotonNetwork.connected == false )

{

return;

}

DoDrawNetworkPositionGizmo();

DoDrawExtrapolatedPositionGizmo();

}

void DoDrawNetworkPositionGizmo()

{

if( m_PositionModel.DrawNetworkGizmo == false || m_PositionControl == null )

{

return;

}

ExitGames.Client.GUI.GizmoTypeDrawer.Draw( m_PositionControl.GetNetworkPosition(),

m_PositionModel.NetworkGizmoType,

m_PositionModel.NetworkGizmoColor,

m_PositionModel.NetworkGizmoSize );

}

void DoDrawExtrapolatedPositionGizmo()

{

if( m_PositionModel.DrawExtrapolatedGizmo == false ||

m_PositionModel.ExtrapolateOption == PhotonTransformViewPositionModel.ExtrapolateOptions.Disabled ||

m_PositionControl == null )

{

return;

}

ExitGames.Client.GUI.GizmoTypeDrawer.Draw( m_PositionControl.GetNetworkPosition() + m_PositionControl.GetExtrapolatedPositionOffset(),

m_PositionModel.ExtrapolatedGizmoType,

m_PositionModel.ExtrapolatedGizmoColor,

m_PositionModel.ExtrapolatedGizmoSize );

}




Trò chơi Tic-Tac-Toe, game đánh caro full source code 53.535 lượt xem

Gõ tìm kiếm nhanh...